home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 51 / Amiga Format CD51 (2000-03-10)(Future Publishing)(GB)[!][issue 2000-04].iso / -in_the_mag- / workbench / term_4.8 / extras / source / gtlayout-source.lha / LT_LayoutMenus.c < prev    next >
C/C++ Source or Header  |  1997-05-08  |  5KB  |  231 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1997 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. #include "Assert.h"
  15.  
  16. struct Menu *
  17. LT_LayoutMenus(LayoutHandle *handle,struct NewMenu *menuTemplate,...)
  18. {
  19.     struct Menu    *Result;
  20.     va_list      VarArgs;
  21.  
  22.     va_start(VarArgs,menuTemplate);
  23.     Result = LT_LayoutMenusA(handle,menuTemplate,(struct TagItem *)VarArgs);
  24.     va_end(VarArgs);
  25.  
  26.     return(Result);
  27. }
  28.  
  29.  
  30. /*****************************************************************************/
  31.  
  32.  
  33. /****** gtlayout.library/LT_LayoutMenusA ******************************************
  34. *
  35. *   NAME
  36. *    LT_LayoutMenusA -- Create a menu according to a template.
  37. *
  38. *   SYNOPSIS
  39. *    Menu = LT_LayoutMenusA(Handle,Template,Tags);
  40. *     D0                      A0     A1      A2
  41. *
  42. *    struct Menu *LT_LayoutMenusA(LayoutHandle *,struct NewMenu *,
  43. *
  44. *        struct TagItem *);
  45. *
  46. *    Menu = LT_LayoutMenus(Handle,Template,...);
  47. *
  48. *    struct Menu *LT_LayoutMenus(LayoutHandle *,struct NewMenu *,...);
  49. *
  50. *   FUNCTION
  51. *    Unlike the corresponding routines in gadtools.library
  52. *    LT_LayoutMenusA() will both create and layout a menu.
  53. *    Also included is locale support.
  54. *
  55. *   INPUTS
  56. *    Handle - Pointer to LayoutHandle structure.
  57. *
  58. *    Template - Address of a ready-to-use NewMenu table to
  59. *        create the menu from.
  60. *
  61. *    Tags - Tagitem list to control menu attributes
  62. *
  63. *    Tags:
  64. *
  65. *    LAMN_FirstLabel (LONG) - Locale ID of the first string to
  66. *        use as a menu title/item/subitem label. This tag
  67. *        works in conjunction with LA_LastLabel.
  68. *
  69. *    LAMN_LastLabel (LONG) - Locale ID of the last string to
  70. *        use as a menu title/item/subitem label. This tag
  71. *        works in conjunction with LA_FirstLabel. The code
  72. *        will loop through FirstLabel..LastLabel and assign
  73. *        the corresponding locale text for each ID to the
  74. *        NewMenu.nm_Label entries. Labels which are already
  75. *        initialized with NM_BARLABEL are skipped.
  76. *
  77. *    LAMN_LabelTable (LONG *) - Pointer to an array of IDs
  78. *        to use for building the menu labels. This requires
  79. *        that a locale hook is provided with the layout handle.
  80. *        The array is terminated by -1.
  81. *
  82. *   RESULT
  83. *    Menu - Pointer to a Menu structure. You can free this
  84. *           using gadtools.library/FreeMenus().
  85. *
  86. ******************************************************************************
  87. *
  88. */
  89.  
  90. struct Menu * LIBENT
  91. LT_LayoutMenusA(REG(a0) LayoutHandle *handle,REG(a1) struct NewMenu *menuTemplate,REG(a2) struct TagItem *TagParams)
  92. {
  93.     struct Menu *menu;
  94.  
  95.     menu = NULL;
  96.  
  97.     if(handle)
  98.     {
  99.         struct TagItem    *tag,*List = TagParams;
  100.         LONG             label,last;
  101.         LONG            *Table = NULL;
  102.         ULONG             allocSize;
  103.         BOOL              foundFirst;
  104.         BOOL              foundLast;
  105.  
  106.         foundFirst    = FALSE;
  107.         foundLast    = FALSE;
  108.  
  109.         while(tag = NextTagItem(&List))
  110.         {
  111.             switch(tag->ti_Tag)
  112.             {
  113.                 case LA_LabelTable:
  114.  
  115.                     Table = (LONG *)tag->ti_Data;
  116.                     break;
  117.  
  118.                 case LA_FirstLabel:
  119.  
  120.                     foundFirst = TRUE;
  121.  
  122.                     label = tag->ti_Data;
  123.                     break;
  124.  
  125.                 case LA_LastLabel:
  126.  
  127.                     foundLast = TRUE;
  128.  
  129.                     last = tag->ti_Data;
  130.                     break;
  131.             }
  132.         }
  133.  
  134.         if(foundFirst == foundLast || Table)
  135.         {
  136.             struct NewMenu *localTemplate = NULL;
  137.  
  138.             if(Table)
  139.             {
  140.                 foundFirst = FALSE;
  141.  
  142.                 if(handle->LocaleHook)
  143.                 {
  144.                     LONG count;
  145.  
  146.                     count = 0;
  147.  
  148.                     while(menuTemplate[count].nm_Type != NM_END)
  149.                         count++;
  150.  
  151.                     count++;
  152.  
  153.                     if(localTemplate = LTP_Alloc(handle,allocSize = sizeof(struct NewMenu) * count))
  154.                     {
  155.                         LONG i;
  156.  
  157.                         CopyMem(menuTemplate,localTemplate,sizeof(struct NewMenu) * count);
  158.  
  159.                         for(i = 0 ; *Table != -1 && i < count ; i++)
  160.                         {
  161.                             if(localTemplate[i].nm_Label != NM_BARLABEL && (localTemplate[i].nm_Type == NM_TITLE || localTemplate[i].nm_Type == NM_ITEM || localTemplate[i].nm_Type == NM_SUB))
  162.                                 localTemplate[i].nm_Label = (STRPTR)CallHookPkt(handle->LocaleHook,handle,(APTR)(*Table++));
  163.                         }
  164.                     }
  165.                 }
  166.  
  167.                 menuTemplate = localTemplate;
  168.             }
  169.  
  170.             if(foundFirst)
  171.             {
  172.                 if(handle->LocaleHook)
  173.                 {
  174.                     LONG count;
  175.  
  176.                     count = 0;
  177.  
  178.                     while(menuTemplate[count].nm_Type != NM_END)
  179.                         count++;
  180.  
  181.                     count++;
  182.  
  183.                     if(localTemplate = LTP_Alloc(handle,allocSize = sizeof(struct NewMenu) * count))
  184.                     {
  185.                         LONG i;
  186.  
  187.                         CopyMem(menuTemplate,localTemplate,sizeof(struct NewMenu) * count);
  188.  
  189.                         if(foundLast)
  190.                             count = last - label + 1;
  191.  
  192.                         for(i = 0 ; i < count ; i++)
  193.                         {
  194.                             if(localTemplate[i].nm_Label != NM_BARLABEL && (localTemplate[i].nm_Type == NM_TITLE || localTemplate[i].nm_Type == NM_ITEM || localTemplate[i].nm_Type == NM_SUB))
  195.                             {
  196.                                 localTemplate[i].nm_Label = (STRPTR)CallHookPkt(handle->LocaleHook,handle,(APTR)label);
  197.  
  198.                                 label++;
  199.                             }
  200.                         }
  201.                     }
  202.                 }
  203.  
  204.                 menuTemplate = localTemplate;
  205.             }
  206.  
  207.             if(menuTemplate)
  208.             {
  209.                 if(menu = CreateMenusA(menuTemplate,NULL))
  210.                 {
  211.                     if(!LayoutMenus(menu,handle->VisualInfo,
  212.                         GTMN_NewLookMenus,    TRUE,
  213.  
  214.                         handle->AmigaGlyph ? GTMN_AmigaKey : TAG_IGNORE,handle->AmigaGlyph,
  215.                         handle->CheckGlyph ? GTMN_Checkmark : TAG_IGNORE,handle->CheckGlyph,
  216.                     TAG_DONE))
  217.                     {
  218.                         FreeMenus(menu);
  219.  
  220.                         menu = NULL;
  221.                     }
  222.                 }
  223.             }
  224.  
  225.             LTP_Free(handle,localTemplate,allocSize);
  226.         }
  227.     }
  228.  
  229.     return(menu);
  230. }
  231.